package com.buddy.learn.network.nio.buffer;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

/**
 * @author WongZP
 */
public class BufferDemo {

    public static void main(String[] args) {
        ByteBuffer wrap = ByteBuffer.wrap("zili".getBytes());
        for (int i = 0; i < 4; i++) {
            System.out.println(wrap.get());
        }

        ByteBuffer byteBuffer = ByteBuffer.allocate(10);

//        for (int i = 0; i < 10; i++) {
//            System.out.println(byteBuffer.get());
//        }
//        System.out.println(byteBuffer.position());
//        byteBuffer.position(9);
        byteBuffer.put("abc".getBytes(StandardCharsets.UTF_8));
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println(byteBuffer.remaining());
        // 切换读模式
        byteBuffer.flip();
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        for (int i=0;i<byteBuffer.limit();i++){
            System.out.println(byteBuffer.get(i));
        }
        byteBuffer.clear();


    }
}
